package mapper;

import dto.ScheduleMasterDTO;
import entity.ScheduleMaster;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * 排班主表SQL提供类
 */
public class ScheduleMasterMapperProvider {

    /**
     * 插入排班信息的SQL
     * @param scheduleMaster 排班主表实体
     * @return SQL语句
     */
    public String insert(ScheduleMaster scheduleMaster) {
        return new SQL() {
            {
                INSERT_INTO("PBGL_SCHEDULE_MASTER");
                if (scheduleMaster.getId() != null) {
                    VALUES("ID", "#{id}");
                }
                if (scheduleMaster.getTypesettingStartTime() != null) {
                    VALUES("TYPESETTING_START_TIME", "#{typesettingStartTime}");
                }
                if (scheduleMaster.getTypesettingEndTime() != null) {
                    VALUES("TYPESETTING_END_TIME", "#{typesettingEndTime}");
                }
                if (scheduleMaster.getCreateUser() != null) {
                    VALUES("CREATE_USER", "#{createUser}");
                }
                if (scheduleMaster.getCreateTime() != null) {
                    VALUES("CREATE_TIME", "#{createTime}");
                }
                if (scheduleMaster.getUpdateUser() != null) {
                    VALUES("UPDATE_USER", "#{updateUser}");
                }
                if (scheduleMaster.getUpdateTime() != null) {
                    VALUES("UPDATE_TIME", "#{updateTime}");
                }
                if (scheduleMaster.getStatus() != null) {
                    VALUES("STATUS", "#{status}");
                }
            }
        }.toString();
    }

    /**
     * 根据ID删除排班信息的SQL
     * @param id 主键ID
     * @return SQL语句
     */
    public String deleteById(String id) {
        return new SQL() {
            {
                DELETE_FROM("PBGL_SCHEDULE_MASTER");
                WHERE("ID = #{id}");
            }
        }.toString();
    }

    /**
     * 批量删除排班信息的SQL
     * @param ids 主键ID列表
     * @return SQL语句
     */
    public String batchDelete(List<String> ids) {
        return new SQL() {
            {
                DELETE_FROM("PBGL_SCHEDULE_MASTER");
                WHERE("ID IN (" + String.join(",", java.util.Collections.nCopies(ids.size(), "?")) + ")");
            }
        }.toString();
    }

    /**
     * 更新排班信息的SQL
     * @param scheduleMaster 排班主表实体
     * @return SQL语句
     */
    public String update(ScheduleMaster scheduleMaster) {
        return new SQL() {
            {
                UPDATE("PBGL_SCHEDULE_MASTER");
                if (scheduleMaster.getTypesettingStartTime() != null) {
                    SET("TYPESETTING_START_TIME = #{typesettingStartTime}");
                }
                if (scheduleMaster.getTypesettingEndTime() != null) {
                    SET("TYPESETTING_END_TIME = #{typesettingEndTime}");
                }
                if (scheduleMaster.getUpdateUser() != null) {
                    SET("UPDATE_USER = #{updateUser}");
                }
                if (scheduleMaster.getUpdateTime() != null) {
                    SET("UPDATE_TIME = #{updateTime}");
                }
                if (scheduleMaster.getStatus() != null) {
                    SET("STATUS = #{status}");
                }
                WHERE("ID = #{id}");
            }
        }.toString();
    }

    /**
     * 发布排班信息的SQL
     * @param id 主键ID
     * @return SQL语句
     */
    public String publish(String id) {
        return new SQL() {
            {
                UPDATE("PBGL_SCHEDULE_MASTER");
                SET("STATUS = 1"); // 1表示已发布
                WHERE("ID = #{id}");
            }
        }.toString();
    }

    /**
     * 根据ID查询排班信息的SQL
     * @param id 主键ID
     * @return SQL语句
     */
    public String selectById(String id) {
        return new SQL() {
            {
                SELECT("*").FROM("PBGL_SCHEDULE_MASTER");
                WHERE("ID = #{id}");
            }
        }.toString();
    }

    /**
     * 查询排班列表的SQL
     * @param scheduleMasterDTO 查询条件
     * @return SQL语句
     */
    public String selectList(ScheduleMasterDTO scheduleMasterDTO) {
        return new SQL() {
            {
                SELECT("*").FROM("PBGL_SCHEDULE_MASTER");
                if (scheduleMasterDTO.getId() != null) {
                    WHERE("ID = #{id}");
                }
                if (scheduleMasterDTO.getTypesettingStartTime() != null) {
                    WHERE("TYPESETTING_START_TIME >= #{typesettingStartTime}");
                }
                if (scheduleMasterDTO.getTypesettingEndTime() != null) {
                    WHERE("TYPESETTING_END_TIME <= #{typesettingEndTime}");
                }
                if (scheduleMasterDTO.getStatus() != null) {
                    WHERE("STATUS = #{status}");
                }
                ORDER_BY("CREATE_TIME DESC");
            }
        }.toString() + " LIMIT " + ((scheduleMasterDTO.getPageNum() - 1) * scheduleMasterDTO.getPageSize()) + ", " + scheduleMasterDTO.getPageSize();
    }

    /**
     * 查询排班总数的SQL
     * @param scheduleMasterDTO 查询条件
     * @return SQL语句
     */
    public String selectCount(ScheduleMasterDTO scheduleMasterDTO) {
        return new SQL() {
            {
                SELECT("COUNT(*)").FROM("PBGL_SCHEDULE_MASTER");
                if (scheduleMasterDTO.getId() != null) {
                    WHERE("ID = #{id}");
                }
                if (scheduleMasterDTO.getTypesettingStartTime() != null) {
                    WHERE("TYPESETTING_START_TIME >= #{typesettingStartTime}");
                }
                if (scheduleMasterDTO.getTypesettingEndTime() != null) {
                    WHERE("TYPESETTING_END_TIME <= #{typesettingEndTime}");
                }
                if (scheduleMasterDTO.getStatus() != null) {
                    WHERE("STATUS = #{status}");
                }
            }
        }.toString();
    }
}